package cn.edu.scau.cmi.tangqingning.filter;

/**
 * 过滤器
 * @author 唐庆宁
 */
import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class EncodingFilter implements Filter {

	/**
	 * 该过滤器用于解决乱码问题
	 *@param req 类型是ServletRequest
	 *@param resp 类型是ServletResponse
	 *@param chain 类型是FilterChain
	 *@throws ServletException 如果一个错误发生，则抛出此异常
	 * @throws IOException 如果一个错误发生，则抛出此异常
	 */
	@Override
	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) resp;
		// 如果用户输入中文，对中文请求内容处理
		request.setCharacterEncoding("utf-8");
		// 将中文输出到页面上，设置响应字符集
		response.setContentType("text/html;charset=utf-8");
		response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");// HTTP
																					// 1.1.
		response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
		response.setDateHeader("Expires", 0); // Proxies.
		chain.doFilter(request, response);
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}

}
